//
//  ARTMacro.h
//  YunDaHome-iOS
//
//  Created by fc_curry on 2019/8/5.
//  Copyright © 2019 fc_curry. All rights reserved.
//

#ifndef ARTMacro_h
#define ARTMacro_h

#import <Foundation/Foundation.h>

typedef void (^CompletionBlock)(id obj);

typedef BOOL(^UDAValidationBlock)(id obj);

// x 系列
#define kDevice_Is_iPhoneXSeries ([ARTPhoneJudgeManager shareManager].iPhoneXSeries)
#define kNavi_Height ((kDevice_Is_iPhoneXSeries) ? 88.0 : 64.0)
// 系统判断
#define IS_IOS_7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7?YES:NO)
#define IS_IOS_8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8?YES:NO)

#define IS_iPhoneXFunc ({\
BOOL isPhoneX = NO;\
if (@available(iOS 11.0, *)) {\
    if (!UIEdgeInsetsEqualToEdgeInsets([UIApplication sharedApplication].delegate.window.safeAreaInsets, UIEdgeInsetsZero)) {\
    isPhoneX = YES;\
    }\
}\
isPhoneX;\
})
// 手机型号判断
#define IS_iPhoneX ((IS_IPHONE_X || IS_IPHONE_Xr || IS_IPHONE_Xs || IS_IPHONE_Xs_Max) ? YES : NO)
// 判断iPhoneX
#define IS_IPHONE_X ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
// 判断iPHoneXr
#define IS_IPHONE_Xr ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(828, 1792), [[UIScreen mainScreen] currentMode].size) : NO)
// 判断iPhoneXs
#define IS_IPHONE_Xs ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
// 判断iPhoneXs Max
#define IS_IPHONE_Xs_Max ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2688), [[UIScreen mainScreen] currentMode].size) : NO)
// 判断iphone5 5s
#define IS_IPHONE5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
// 判断4s
#define IS_IPHONE4S ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
// 判断 6 6s 7 8
#define IS_IPHONE6S ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
// 判断 6p 7p 8p
#define IS_IPHONE6P ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(828, 1472), [[UIScreen mainScreen] currentMode].size) : NO)

//字符串比较
#define J_StringCompare(stringA,stringB)  [stringA isEqualToString:stringB]

#define J_INTEGRE_TO_STRING(A)             [NSString stringWithFormat:@"%ld",(long)(A)]
#define J_UINTEGRE_TO_STRING(A)            [NSString stringWithFormat:@"%lu",(unsigned long)(A)]
#define J_INT_TO_STRING(A)                 [NSString stringWithFormat:@"%d",(int)(A)]
#define J_FLOAT_TO_STR(A)                  [NSString stringWithFormat:@"%.2f",(double)(A)]
#define j_NumToFix2(A)     A?[NSString stringWithFormat:@"%.2f",[A floatValue]]:@"0"
#define J_COMBAIN_STRING(A,B) [NSString stringWithFormat:@"%@%@",A,B]
#define J_Salary_Combine(A) A?J_COMBAIN_STRING(@"￥", j_NumToFix2(A)):@"0"
#define J_Safe_Null(A) kISNullString(A)?@"0":[A containsString:@"."]?[NSString stringWithFormat:@"%.1f",[A floatValue]]:A

// 字符串是否为空
#define kISNullString(str) (([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 || (![str isKindOfClass:[NSString class]]) || [str isEqualToString:@"<null>"] || [str isEqualToString:@"null"] || [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]== 0) ? YES : NO )

// 数组是否为空
#define kISNullArray(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0 ||[array isEqual:[NSNull null]])

// 字典是否为空
#define kISNullDict(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0 || [dic isEqual:[NSNull null]])

// 是否是空对象
#define kISNullObject(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))

//判断对象是否为空,为空则返回默认值
#define kGetNullDefaultObj(_value,_default) ([_value isKindOfClass:[NSNull class]] || !_value || _value == nil || [_value isEqualToString:@"(null)"] || [_value isEqualToString:@"<null>"] || [_value isEqualToString:@""] || [_value length] == 0)?_default:_value

// 解决iOS10以上，系统log打印不全
#define NSSLog(format, ...) printf("class: <%p %s:(%d) > method: %s \n%s\n", self, [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, __PRETTY_FUNCTION__, [[NSString stringWithFormat:(format), ##__VA_ARGS__] UTF8String] )



// 获取屏幕宽度与高度受横屏,竖屏影响
#define J_SCREEN_WIDTH        [UIScreen mainScreen].bounds.size.width

#define J_SCREEN_HEIGHT       [UIScreen mainScreen].bounds.size.height

#define J_SCREEN_BOUNDS       [UIScreen mainScreen].bounds

//#define J_Width(X) roundf((J_SCREEN_WIDTH) * ((X) / 375.0f)) // 5s  6,7,8,x   6p,7p,8p
//
//#define J_Height(Y) roundf((J_SCREEN_HEIGHT) * ((Y) / 667.0f))  // 5s  6,7,8  6p,7p,9p   x

#define J_Width(X) roundf((Scale) * ((X) * (0.85))) // 5s  6,7,8,x   6p,7p,8p

#define J_Height(Y) roundf((Scale) * ((Y) * (0.85)))

#define J_SafeAreaTop_Height (IS_iPhoneX ? 84 : 64)  // 顶部距离安全区的距离

#define J_SafeAreaBottom_Height (IS_iPhoneX ? 83 : 49) // 底部距离安全区的距离

#define J_FIT_BOTTOM(Y) ((IS_IPHONE_X || IS_IPHONE_Xr || IS_IPHONE_Xs || IS_IPHONE_Xs_Max) ? Y+34 : Y)

#define J_FIT_HEIGHT(Y) ((IS_IPHONE_X || IS_IPHONE_Xr || IS_IPHONE_Xs || IS_IPHONE_Xs_Max) ? Y+34 : Y)

// 导航栏
#define J_StatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height //状态栏高度

#define J_TabBarHeight (J_StatusBarHeight > 20 ? 83:49) //底部tabbar高度

#define J_SafeAreaBottomHeight (J_StatusBarHeight > 20 ? 34:0) //底部tabbar高度

#define J_BarHeight  44.0

#define J_NavBarHeight (J_BarHeight +  J_StatusBarHeight) //整个导航栏高度

///------ iOS Device Type ------
#define kDevice_Is_iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define kDevice_Is_iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
#define kDevice_Is_iPhone6P ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
#define kDevice_Is_iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
#define kDevice_Is_iPhoneXs ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
#define kDevice_Is_iPhoneXR ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(828, 1792), [[UIScreen mainScreen] currentMode].size) : NO)
#define kDevice_Is_iPhoneXM ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2688), [[UIScreen mainScreen] currentMode].size) : NO)
// x 系列
#define kDevice_Is_iPhoneXSeries ([ARTPhoneJudgeManager shareManager].iPhoneXSeries)
#define kNavi_Height ((kDevice_Is_iPhoneXSeries) ? 88.0 : 64.0)


// 获取一个像素
#define J_PixelOne  1.0/[[UIScreen mainScreen] scale]
#define J_AdjustLineOffset   ((1 / [UIScreen mainScreen].scale) / 2)

//颜色
#define ssRGB(r, g, b)             [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:1.0]
#define ssRGBAlpha(r, g, b, a)     [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:(a)]

// 弱引用/强引用
#define J_WeakSelf(type)  __weak typeof(type) weak##type = type;
#define J_StrongSelf(type)  __strong typeof(type) type = weak##type;

// View 圆角
#define J_ViewRadius(View, Radius)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES]

// View加边框
#define J_ViewBorder(View, BorderColor, BorderWidth)\
\
View.layer.borderColor = BorderColor.CGColor;\
View.layer.borderWidth = BorderWidth;

// frame
#define J_Frame(x,y,width,height)  CGRectMake(x, y, width, height)
#define J_FrameWithSize(width,height)  CGRectMake(0, 0, width, height)
#define J_FrameWithOrigin(x,y)  CGRectMake(x, y, 0, 0)

// scale
#define J_SCALE ([[UIScreen mainScreen] bounds].size.height > 480)?[[UIScreen mainScreen] bounds].size.height / 568.0:1.0
#define Scale J_TabBarHeight > 49 ?1.30:J_SCALE

// 安全边距
#define     J_SAFEAREA_INSETS     \
({   \
    UIEdgeInsets edgeInsets = UIEdgeInsetsZero; \
    if (@available(iOS 11.0, *)) {      \
        edgeInsets = [UIApplication sharedApplication].keyWindow.safeAreaInsets;     \
    }   \
    edgeInsets;  \
})\

// 字体
#define J_Font(font)[UIFont systemFontOfSize:(font)*(Scale)*(0.8)]
#define j_RegularFont(font) [UIFont systemFontOfSize:(font)*(Scale)*(0.8) weight:UIFontWeightRegular]
#define J_BoldFont(font) [UIFont systemFontOfSize:(font)*(Scale)*(0.8) weight:UIFontWeightBold]
#define J_MediumFont(font) [UIFont systemFontOfSize:(font)*(Scale)*(0.8) weight:UIFontWeightMedium]
#define j_SemiboldFont(font) [UIFont systemFontOfSize:(font)*(Scale)*(0.8) weight:UIFontWeightSemibold]

//字体粗细
#define J_Regular(font) [UIFont systemFontOfSize:font weight:UIFontWeightRegular];
#define J_Medium(font) [UIFont systemFontOfSize:font weight:UIFontWeightMedium];
#define J_Bold(font) [UIFont systemFontOfSize:font weight:UIFontWeightBold];
#define J_Heavy(font) [UIFont systemFontOfSize:font weight:UIFontWeightHeavy];
// 加载本地图片
#define J_ImageNamed(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]
#define J_PlaceholderImage J_ImageNamed(@"message_pic_failed")
 
// 沙盒目录文件
// 获取temp
#define J_Path_Tmp NSTemporaryDirectory()

// 获取沙盒 Document
#define J_Path_Document [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]

// 获取沙盒 Cache
#define J_Path_Cache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

// UserDefault存储对象
#define J_UserDefault_SetObjectForKey(__VALUE__,__KEY__) \
{\
[[NSUserDefaults standardUserDefaults] setObject:__VALUE__ forKey:__KEY__];\
[[NSUserDefaults standardUserDefaults] synchronize];\
}

// UserDefault获得存储的对象
#define J_UserDefault_GetObjectForKey(__KEY__)  [[NSUserDefaults standardUserDefaults] objectForKey:__KEY__]

// UserDefault  删除对象
#define J_UserDefault_RemoveObjectForKey(__KEY__) \
{\
[[NSUserDefaults standardUserDefaults] removeObjectForKey:__KEY__];\
[[NSUserDefaults standardUserDefaults] synchronize];\
}

// 清空UserDefault(去除首次启动页key)
#define J_UserDefault_ClearAllObject \
{\
NSDictionary * dict = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];\
for (id key in dict)\
{\
if (![key isEqualToString:@"isFrist_lanch"]) {\
[[NSUserDefaults standardUserDefaults] removeObjectForKey:key];\
}\
}\
[[NSUserDefaults standardUserDefaults] synchronize];\
}

// 通知相关
#define J_Post_Notify(name) \
[[NSNotificationCenter defaultCenter] postNotificationName:name object:nil]

#define J_Post_Notify_Obj(name, obj) \
[[NSNotificationCenter defaultCenter] postNotificationName:name object:obj]

#define J_Post_Notify_ObjAndUserInfo(name, obj, us) \
[[NSNotificationCenter defaultCenter] postNotificationName:name object:obj userInfo:us]

#define J_Add_Notify(observer, _selector, _name, obj) \
[[NSNotificationCenter defaultCenter] addObserver:observer selector:_selector name:_name object:obj]

#define J_Remove_Notify(observer) \
[[NSNotificationCenter defaultCenter] removeObserver:observer]

// 十六进制色值转颜色
#define J_ColorFromHEX(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#define weakSelf(type) __weak __typeof__(type) weakSelf = type;
#define strongSelf(type) __strong __typeof__(type) strongSelf = type; if (nil == strongSelf) return;

#define ContentH ((IS_IPHONE6S || IS_IPHONE6P) ? 58 : 70)
#define CustomH ((IS_IPHONE6S || IS_IPHONE6P) ? 50 : 70)
#define TopPadding ((IS_IPHONE6S || IS_IPHONE6P) ? 10 : 16)

// GCD - 主线程执行
static inline void J_Run_mainQueue(void (^block)(void)){
    
    if ([NSThread isMainThread]) {
        block();
    } else {
        dispatch_async(dispatch_get_main_queue(), block);
    }
}

// GCD - 异步线程执行
static inline void J_Run_globalQueue(void (^block)(void)){
    
    if (![NSThread isMainThread]) {
        block();
    } else {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
    }
}

// 如果index 不越界 按index 取 如果index 越界 按最后一位取
static inline id GetObjectFromArray(NSArray *array ,NSInteger index){
    id obj = array[index < array.count ? index : array.count - 1];
    return obj;
}

static inline id GetObjectFromDic(NSDictionary *dic ,id key){
    if (![dic.allKeys containsObject:key]) {
        return @"";
    }
    
    id obj = [dic objectForKey:key];
    return obj ? obj : @"";
}

#endif /* YDMacroConfig_h */
